'use strict';

const db = uniCloud.database();
const collection = db.collection('food-categories');

exports.main = async (event, context) => {
  const { categoryId } = event.data || {};
  
  if (!categoryId) {
    return {
      code: 400,
      message: '缺少分类ID',
      data: null
    };
  }
  
  try {
    // 查询单个分类详情
    const result = await collection.doc(categoryId).get();
    
    if (!result.data || result.data.length === 0) {
      return {
        code: 404,
        message: '分类不存在',
        data: null
      };
    }
    
    return {
      code: 200,
      message: '查询成功',
      data: result.data[0]
    };
  } catch (error) {
    console.error('查询分类详情失败:', error);
    return {
      code: 500,
      message: '查询失败',
      data: null
    };
  }
};